
from machine import Pin # 从machine模块导入Pin，Timer子模块
from ssd1306 import SSD1306_I2C     #从ssd1306模块中导入SSD1306_I2C子模块
from sh1106 import SH1106_I2C     #从sh1106模块中导入SH1106_I2C子模块
from machine import SoftI2C         #从machine模块导入I2C、Pin子模块
from GPIO import *
from input_chinese import OLEDController
from OLED import oled

class texttype:
    cn,en=range(2)
    
title="线损排查装置"
bledisconnected="蓝牙已断开"
pleaseconnectble="请先连接蓝牙设备"
bleconnected ="蓝牙已连接"
pleasereadmeter="请双击按钮抄表"
r485Error ="红外通讯异常"
tryagainreadmeter="双击按钮重试抄表"   
nowread="正在读取"     
threephasemeter="三相电表"
onephasemeter="单相电表"
abnormalstatus= "电表状态异常"
details ="信息详见手机端"
normalstatus = '电表运行正常'
currentPA ='当前正向有功电量'
currentRA ='当前反向有功电量'
upkeypoweroff = "抬起按键关机"
ConnectBluetoothfirst="抄表请先连接蓝牙"
class pixel():
    def __init__(self,x,y):
        self.x=x
        self.y=y
        
class lineinfo():
    def __init__(self,info,pixel:pixel,texttype:texttype=texttype.en):
        self.info =info
        self.pixel =pixel
        self.texttype =texttype
    
welcomepagedisconnected=[
        lineinfo(title,pixel(0,1),texttype.cn),
        lineinfo(bledisconnected,pixel(0,18),texttype.cn),
        lineinfo(pleaseconnectble,pixel(0,35),texttype.cn),
        lineinfo("",pixel(0,35+16+1),texttype.en)
        ]

welcomepageconnected=[
        lineinfo(title,pixel(0,1),texttype.cn),
        lineinfo(bleconnected,pixel(0,18),texttype.cn),
        lineinfo(pleasereadmeter,pixel(0,35),texttype.cn),
        lineinfo("",pixel(0,35+16+1),texttype.en)
        ]

#调用BLE 设置ESP32被发现的蓝牙名称 名称为主程序传参传入    
def init_oled(typedriver):
        i2c = SoftI2C(sda=Pin(psda), scl=Pin(pscl))   #I2C初始化：sda-->42, scl -->40
        if typedriver == 1:
            #OLED显示屏初始化：128*64分辨率,OLED的I2C地址是0x3c
            _oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
        elif typedriver == 2:
            #OLED显示屏初始化：128*64分辨率,OLED的I2C地址是0x3c    
            _oled = SH1106_I2C(128, 64, i2c, addr=0x3c,rotate =180)
        return _oled    



class display():
    def __init__(self):
        self.oled = init_oled(2)
        self.oledcn = OLEDController(self.oled,2)#创建实例\
        
    def show(self,info:list[lineinfo]):
        self.oled.fill(0)
        self.oledcn.fill(0)
        for one in info:
            if one.texttype == texttype.cn:                
                self.oledcn.text(one.info,one.pixel.x,one.pixel.y)
            else:
                self.oled.text(one.info,one.pixel.x,one.pixel.y)
        self.oled.show()
        
yini=0
xini=0
interval=2
cnrowheight=16+interval
enrowheight=8+interval 
maxscreenheight=64

class PageControl():
    pages:list[list[lineinfo]]
          

    def  __init__(self,inDisplay:display):
        self.currentpagenumber=-1
        self.pages =[]
        self.Display =inDisplay
        self.x =xini
        self.y =yini
        
    def show(self):
        count = len(self.pages)
        if(count==0):
             self.currentpagenumber =-1
             return
        self.currentpagenumber=0
        
        #print(self.pages[self.currentpagenumber])
        self.Display.show(self.pages[self.currentpagenumber])
         
    def nextshow(self):
        #print('换页')
        #print(self.currentpagenumber)
        self.currentpagenumber=self.currentpagenumber+1
        count = len(self.pages)
        if(count==0):
            self.currentpagenumber =-1
            return
        if(self.currentpagenumber >=count):
            self.currentpagenumber =0
        self.Display.show(self.pages[self.currentpagenumber])
        
    def clear(self):
        self.currentpagenumber =-1
        self.pages.clear()
        self.x =xini
        self.y =yini
        
    def newpage(self):       
        self.append([])
        self.x =xini
        self.y =yini
    
    def append(self,inlist:list[lineinfo])->list[lineinfo]:
        self.pages.append(inlist)
        return inlist
        
    def last(self)->list[lineinfo]:
        if(len(self.pages)>0):
            return self.pages[len(self.pages)-1]
    def addlinecn(self,text):
        self.addline(text,texttype.cn)

    def addline(self,text,_texttype:texttype=texttype.en):
        
        if(len(self.pages) == 0):
            self.newpage()
        #本次输出的高度
        if(_texttype == texttype.cn):
            height=cnrowheight-interval
        else:
            height=enrowheight-interval
        #判断本次输出是否大于屏幕最大值如果大于则增加新页
        if(self.y+height>maxscreenheight):
            self.y=yini
            self.newpage()
        #走到最后一页，添加一行
        page = self.last()      
        oneline = lineinfo(text,pixel(self.x,self.y),_texttype)
        page.append(oneline)
        #调整下一次输出位置
        if(_texttype == texttype.cn):
            self.y = self.y +cnrowheight
        else:
            self.y = self.y +enrowheight
            